package com.fristvue.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fristvue.po.Goods;
import com.fristvue.po.Util.Page;
import com.fristvue.po.Util.Utils;
import com.fristvue.service.GoodsService;

/**
 * 商品的前端控制层
 * @author
 */
@RestController  //返回的时前端json数据
public class GoodsController {
	@Autowired
	private GoodsService goodsService;
	
	/**
	 * 分页操作,查询全部数据
	 * @param page
	 * @return
	 */
	@RequestMapping("/goods/{page}/{size}")
    public Page<Goods> FindGoodsAll(@PathVariable("page") Integer page,
    		@PathVariable("size") Integer size, @RequestBody Utils goods){
        Integer num = page * size;
        return this.goodsService.AllGoodsLimit(num, size, goods);
    }
		
	/**
	 * 删除指定的商品
	 * @param id
	 * @return
	 */
	@DeleteMapping("/deleteGoods")
    public boolean delteById(Integer id){
        return this.goodsService.deleteByPrimaryKey(id);
    }
	/**
	 * 查询出对应的id商品,用于修改
	 * @param id
	 * @return
	 */
	@RequestMapping("/findGoods")
    public Goods findGoods(Integer id){
        return this.goodsService.selectByPrimaryKey(id);
    }
	/**
	 * 修改商品的详细信息
	 * @param goods
	 * @return
	 */
	@PutMapping("/updateGoods")
    public int updateGoods(@RequestBody Goods goods){
        return this.goodsService.updateByPrimaryKeySelective(goods);
    }
	
	/**
	 * 添加商品
	 * @param goods
	 * @return
	 */
	@PutMapping("/addGoods")
	public boolean addGoods(@RequestBody Goods goods) {
		if(goods.getCount() > 0) {
			goods.setOnSale("yes");
		}else {
			goods.setOnSale("no");
		}
		goods.setGoodsImg("/images/front/nurse/xifas.jpg");
		return this.goodsService.addGoods(goods);
	}
	/**
	 *只传商品id和商品名字
	 * @return
	 */
	@GetMapping("whereGoods")
	public List<Goods> whereGoods(){
		return this.goodsService.whereGoods();
	}
}
